<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/5/2
 * Time: 下午8:31
 */
namespace Common\Model;
use Think\Model;

class PosterspaceModel extends Model{

    //无表前缀的菜单表名
    protected $table;
    //有前缀的菜单表
    protected $menuTable;
    //表前缀
    protected $prefix;

    /**构造函数初始化数据
     * @param string $adminTable
     * @param string $prefix
     */
    public function __construct($menuTable,$prefix = ''){
        parent::__construct();
        $this->prefix = empty($prefix) ? C('DB_PREFIX') : $prefix;
        $this->table = $menuTable;
        $this->menuTable = $this->prefix.$menuTable;
    }
    public function addMenu($data){
        if(empty($data['title'])){
            return ['status' => 1,'msg' => '版位名称不能为空'];
        }
        $id = empty($data['id']) ? 0 : $data['id'];
        if($id == 0){
            $poster = M($this->table)->field('id')->where(['name' => $data['title']])->count();
            if($poster > 0){
                return ['status' => 1,'msg' => '版位名称不能重复'];
            }
        }
        $title = empty($data['title']) ? '' : $data['title'];
        $type = empty($data['type']) ? 'banner' : $data['type'];
        $width = empty($data['width']) ? 0 : $data['width'];
        $height = empty($data['height']) ? 0 : $data['height'];
        $description = empty($data['description']) ? '' : $data['description'];
        $data = ['id'=>$id,'name' => $title,'type' => $type,'width' => $width,'height' => $height,'description' => $description];
        $id = M($this->table)->add($data,[],true);
        if($id){
            return ['status' => 0,'msg' => '成功'];
        }else{
            return ['status' => 1,'msg' => '失败'];
        }
    }
    public function menuList($id){
        if(empty($id)){
            return ['status' => 1,'msg' => '非法操作'];
        }
        $data = M($this->table)->where(['id' => $id])->select();
        if(empty($data)){
            return ['status' => 1,'msg' => '不存在'];
        }else{
            return ['status' => 0,'msg' => '成功','data' => $data];
        }
    }
    public function del($id){
        if(empty($id)){
            return ['status' => 1,'msg' => '删除异常'];
        }
        $table = M($this->table);
        $del = $table->where(['id' => $id])->delete();
        if($del){
            return ['status' => 0,'msg' => '删除成功'];
        }else{
            return ['status' => 1,'msg' => '删除失败'];
        }
    }
}